<template>
  <div class="app-container">
    <avue-crud
      ref="crud"
      :page="page"
      :data="tableData"
      :table-loading="tableLoading"
      :option="tableOption"
      @on-load="getPage"
      @refresh-change="refreshChange"
      @sort-change="sortChange"
      @search-change="searchChange"
      @selection-change="selectionChange"
    >
      <template slot="menu" slot-scope="scope">
        <el-button
          type="text"
          size="small"
          plain
          v-hasPermi="['book:apply:remark']"
          @click="handleRemark(scope.row)"
          >备注
        </el-button>
      </template>
    </avue-crud>
  </div>
</template>

<script>
import { checkPermi, checkRole } from "@/utils/permission";
import { getPage, getObj, addObj, putObj, delObj } from "@/api/book/apply";
import { tableOption } from "@/const/crud/book/apply";

export default {
  name: "bookApply",
  data() {
    return {
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条
        ascs: [],
        descs: [],
      },
      paramsSearch: {},
      tableLoading: false,
      tableOption: tableOption,
      selectionData: [],
    };
  },
  created() {},
  methods: {
    selectionChange(list) {
      this.selectionData = list;
    },
    searchChange(params, done) {
      params = this.filterForm(params);
      this.paramsSearch = params;
      this.page.currentPage = 1;
      this.getPage(this.page, params);
      done();
    },
    sortChange(val) {
      let prop = val.prop
        ? val.prop.replace(/([A-Z])/g, "_$1").toLowerCase()
        : "";
      if (val.order == "ascending") {
        this.page.descs = [];
        this.page.ascs = prop;
      } else if (val.order == "descending") {
        this.page.ascs = [];
        this.page.descs = prop;
      } else {
        this.page.ascs = [];
        this.page.descs = [];
      }
      this.getPage(this.page);
    },
    getPage(page, params) {
      this.tableLoading = true;
      getPage(
        Object.assign(
          {
            pageNum: page.currentPage,
            pageSize: page.pageSize,
            descs: this.page.descs,
            ascs: this.page.ascs,
            appType: "2",
          },
          params,
          this.paramsSearch
        )
      )
        .then((response) => {
          this.tableData = response.rows;
          this.page.total = response.total;
          this.page.currentPage = page.currentPage;
          this.page.pageSize = page.pageSize;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    handleRemark(row, type) {
      this.$prompt("请输入备注", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /\S/,
        inputErrorMessage: "输入不能为空",
      }).then(({ value }) => {
        this.tableLoading = true;
        putObj({ id: row.id, remarks: value })
          .then((response) => {
            this.tableLoading = false;
            if (response.code == 200) {
              this.$message({
                showClose: true,
                message: "修改成功",
                type: "success",
              });
              this.getPage(this.page);
            } else {
              this.$message.error(response.msg);
            }
          })
          .catch(() => {
            this.tableLoading = false;
          });
      });
    },

    /**
     * 刷新回调
     */
    refreshChange(page) {
      this.getPage(this.page);
    },
  },
};
</script>
